package com.open.capacity.trade.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.netflix.discovery.converters.Auto;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Records;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.trade.feign.TradeFeignClient;
import com.open.capacity.trade.model.RecordsExcel;
import com.open.capacity.trade.service.RecordsService;
import com.open.capacity.trade.vo.TradeRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/records")
@Api(tags = "交易中心API")
public class TradeRecordsController {
    @Autowired
    private RecordsService recordsService;
    @GetMapping("/records")
    public PageResult<Records> findRecords(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
//            PageResult<Records> records = recordsService.findRecords(params);
            PageResult<Records> records = recordsService.findAll();
            List<Records> objects = new ArrayList();
            Records re= new Records();
            re.setCompanyName("万息");
            re.setPosition("Java开发");
//            re.setPositions(5);
            re.setEntrys(3);
            objects.add(0,re);
            PageResult<Records> objectPageResult = new PageResult<>();
            objectPageResult.setData(objects);
            System.out.println(records);
            return records ;
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @GetMapping("/findAllRecords")
    @ApiOperation(value = "查询所有的交易记录")
    public PageResult<TradeRecordsVo> findAllRecords(@RequestParam Map<String,Object> params)throws ControllerException {
        try{
            return recordsService.findAllRecords(params);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @GetMapping("/updateEnabled")
    @ApiOperation(value = "状态改变，是否还在招聘")
    public Result updateEnabled(@RequestParam Map<String,Object> params) throws ControllerException{
        try{ //false 1 关闭 true 热招中 0
            return recordsService.updateEnabled(params);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @DeleteMapping("/deleteRecords/{id}/{recordsId}")
    @ApiOperation(value = "删除交易记录，财务权限")
    public Result deleteRecords(@PathVariable Long id,@PathVariable Long recordsId) throws ControllerException{
        try {
            recordsService.deleteRecords(id,recordsId);
            return Result.succeed("操作成功");
        }catch (Exception e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "修改交易记录,")
    public Result saveOrUpdate(@RequestBody Records records) throws ControllerException{
        //只能修改招聘人数和单价
        try{
         return recordsService.saveOrUpdate(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/exportRecords")
    @ApiOperation(value = "导出全部的交易记录信息Excel")
    public void exportRecords(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {//todo
//            存在数组越界
            List<RecordsExcel> result = recordsService.findAllRecordsExcel(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("交易记录导出", "交易记录"),
                    RecordsExcel.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }

    }
    @PostMapping("/insertRecords")
    @ApiOperation(value = "新增交易中心")
//    当其它模块在满足条件的情况下调用此接口进行新增
    public Result insertRecords(@RequestBody Records records) throws ControllerException{
        try{
            return recordsService.insertRecords(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updateRecordsAccepted")
    @ApiOperation(value = "修改交易记录的已录人数")
    public Result updateRecordsAccepted(@RequestBody Records records) throws ControllerException{
        try {
            return recordsService.updateRecordsAccepted(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updateRecordsBasic")
    @ApiOperation(value = "修改交易记录的一些基本信息")
    public Result updateRecordsBasic(@RequestBody Records records) throws ControllerException{
        try {
            return recordsService.updateRecordsBasic(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updatePI")
    @ApiOperation(value = "修改招聘人数，单价")
//   交易记录不存在修改
    public Result updatePIN(@RequestBody Records records) throws ControllerException{
        try {
            return recordsService.saveOrUpdate(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updateEntrys")
    @ApiOperation(value = "修改已录人数")
//    交易记录不存在修改
    public Result updateEntry(@RequestBody Records records) throws ControllerException{
        try {
            return recordsService.updateEntry(records);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

}
